package com.ddone.finance.controller.admin;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ddone.common.exception.Assert;
import com.ddone.common.result.Response;
import com.ddone.common.result.ResponseEnum;
import com.ddone.finance.pojo.entity.IntegralGrade;
import com.ddone.finance.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author ddone
 * @since 2024-04-10
 */
@RestController
@RequestMapping("/core/admin/integralGrade")
@Api(tags = "积分等级管理模块")
// @CrossOrigin
public class AdminIntegralGradeController {

    @Resource
    private IntegralGradeService integralGradeService;


    @ApiOperation("分页查询")
    @GetMapping("/{page}/{size}")
    public Response<Object> page(@ApiParam(value = "当前页", required = true, example = "1")
                                 @PathVariable("page") Long page,
                                 @ApiParam(value = "每页显示记录", required = true, example = "5")
                                 @PathVariable("size") Long size) {
        Page<IntegralGrade> pageParam = new Page<>(page, size);
        // 分页可以通过last 查询
        // Wrappers.lambdaQuery(IntegralGrade.class).last()
        return Response.ok(integralGradeService.page(pageParam, null));
    }

    @ApiOperation("查询所有数据")
    @GetMapping
    public Response<Object> list(){
        return Response.ok(integralGradeService.list());
    }
    @ApiOperation(("根据ID查询积分等级"))
    @GetMapping("/{id}")
    public Response<Object> getById(
            @ApiParam(value = "积分ID", required = false, example = "1")
            @PathVariable("id") Long id) {
        IntegralGrade integralGrade = integralGradeService.getById(id);
        if (Objects.isNull(integralGrade)) {
            return Response.fail().message("该积分对象不存在");
        }
        return Response.ok(integralGrade);
    }

    @ApiOperation("按照ID删除积分等级")
    @DeleteMapping("/{id}")
    public Response<Object> removeById(
            @ApiParam(value = "积分等级ID", required = true)
            @PathVariable("id") Long id) {
        boolean removeFlag = integralGradeService.removeById(id);
        if (removeFlag) {
            return Response.ok().message("删除成功");
        }
        return Response.fail().message("删除失败");
    }

    @ApiOperation("新增积分等级")
    @PostMapping
    public Response<Object> save(
            @ApiParam(value = "积分等级对象", required = true)
            @RequestBody IntegralGrade integralGrade) {
        boolean result = integralGradeService.save(integralGrade);
        if (result) {
            return Response.ok().message("保存成功");
        }
        return Response.fail().message("保存失败");
    }

    @ApiOperation("更新积分等级")
    @PutMapping
    public Response<Object> updateById(
            @ApiParam(value = "积分等级对象", required = true)
            @RequestBody IntegralGrade integralGrade) {
        Assert.notNull(integralGrade.getBorrowAmount(),ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
        Assert.notNull(integralGrade.getId(),ResponseEnum.ID_IS_EMPTY);
        boolean result = integralGradeService.updateById(integralGrade);
        if (result) {
            return Response.ok().message("修改成功");
        }
        return Response.fail().message("修改失败");

    }

}

